/**
 * 文件：ModifyCostPanel.java
 * 说明：用于修改学生缴费信息
 * 作者：古通基
 * 时间：1211-09-14
 */

package com.java.component;

import java.sql.*;
import java.awt.*;
import javax.swing.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class ModifyCostPanel extends JPanel implements ActionListener,MouseListener
{
	private static final long serialVersionUID = -8783306641177719926L;
	private JLabel sNo = new JLabel("学    号：");                                //静态标签
	private JLabel sName = new JLabel("姓    名：");
	private JLabel coCharge = new JLabel("应缴费：");        
	private JLabel coShi = new JLabel("实缴费：");       
	private JLabel coQian = new JLabel("欠    费：");    
	
	private JTextField no = new JTextField("请输入五位学号",10);         //文本输入
	private JTextField name = new JTextField("",10);                           
	String[] charges = {"4300","4800","5200","6400","7200","8000"};
	private JComboBox charge = new JComboBox(charges);                      
	private JTextField shi = new JTextField("",10);                               
	private JTextField qian = new JTextField("",10);
	
    private JButton query = new JButton("查询");                               //声明按钮
    private JButton ok = new JButton("确定");
    private JButton modify = new JButton("修改");
    	
    private JPanel JPcomon1 = new JPanel();                                      //声明面板
    private JPanel JPcomon2 = new JPanel();
    private JPanel JPcomon3 = new JPanel();
    private JPanel JPcomon4 = new JPanel();
    private JPanel JPcomon5 = new JPanel();
    private JPanel JPbutton = new JPanel();
    
    public ModifyCostPanel()                                                               //构造函数
    {
    	sNo.setHorizontalAlignment(SwingConstants.CENTER);
    	sName.setHorizontalAlignment(SwingConstants.CENTER);
    	coCharge.setHorizontalAlignment(SwingConstants.CENTER);
    	coShi.setHorizontalAlignment(SwingConstants.CENTER);
    	coQian.setHorizontalAlignment(SwingConstants.CENTER);
    	
    	JPcomon1.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon1.add(sNo);
    	JPcomon1.add(no);
    	JPcomon1.add(query);
    	JPcomon2.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon2.add(sName);
    	JPcomon2.add(name);
    	JPcomon3.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon3.add(coCharge);
    	JPcomon3.add(charge);
    	JPcomon4.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon4.add(coShi);
    	JPcomon4.add(shi);
    	JPcomon5.setLayout(new FlowLayout(FlowLayout.LEFT,12,5));
    	JPcomon5.add(coQian);
    	JPcomon5.add(qian);

    	JPbutton.setLayout(new FlowLayout(FlowLayout.CENTER,12,5));
    	JPbutton.add(modify);
    	JPbutton.add(ok);
    	
    	this.setLayout(new GridLayout(10,1));
    	this.add(JPcomon1);
    	this.add(JPcomon2);
    	this.add(JPcomon3);
    	this.add(JPcomon4);
    	this.add(JPcomon5);
    	this.add(JPbutton);
    	
		name.setEditable(false);                                                               //初始化数据保护
		qian.setEditable(false);
    	
    	query.addActionListener(this);                                                     //事件监听        
    	ok.addActionListener(this);
    	modify.addActionListener(this);
    	no.addMouseListener(this);
    }
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getActionCommand().equals("查询"))
		{
			QueryDataBase();
			no.setEditable(false);
			charge.setEnabled(false);
			shi.setEditable(false);
		}
		else if(e.getActionCommand().equals("修改"))
		{
			charge.setEnabled(true);
			shi.setEditable(true);
		}
		else if(e.getActionCommand().equals("确定"))
		{
			Ok();
			no.setEditable(true);
		}
	}
	
	private void QueryDataBase()
	{
		ResultSet rs = null;
		try
		{
			BaseSQL cost = null;
			cost = new BaseSQL();
			String sql = "select * from cost where sno = '"+Integer.parseInt(no.getText())+"'"; 
		    rs = cost.executeSelect(sql);
		    if(rs.next())
    		{
    			if(Integer.parseInt(no.getText())==rs.getInt("sno"))
    			{
    				name.setText(rs.getString("sname"));
    				charge.setSelectedItem(rs.getString("charge"));
    				shi.setText(rs.getString("shi"));
    				qian.setText(Integer.toString(Integer.parseInt(rs.getString("charge"))-Integer.parseInt(rs.getString("shi"))));		
        		}    				
       		} 
    		else
    		{
    			JOptionPane.showMessageDialog(null,"不存在学号为："+no.getText()+"的学生","缴费错误",JOptionPane.INFORMATION_MESSAGE);
    		}
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
	private void Ok()
	{
		try
		{
			BaseSQL cost = null;
			cost = new BaseSQL();
			String sql = "update cost" +
					" set sname='"+name.getText()+"',charge= '"+charge.getSelectedItem()+"',shi='"+shi.getText()+"'  where sno = '"+Integer.parseInt(no.getText())+"'";			
			cost.executeDML(sql);
			qian.setText(Integer.toString(Integer.parseInt((String) charge.getSelectedItem())-Integer.parseInt(shi.getText().trim())));
		    JOptionPane.showMessageDialog(this, "修改成功", "修改住宿信息",JOptionPane.INFORMATION_MESSAGE);
		}
		catch(SQLException evt)
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		} 
		catch (ClassNotFoundException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
		catch (NumberFormatException evt) 
		{
			JOptionPane.showMessageDialog(this, evt, "系统错误",JOptionPane.INFORMATION_MESSAGE);
		}
	}	
	
    public void mouseClicked(MouseEvent e) { }                                       //鼠标事件
    public void mouseReleased(MouseEvent e) { }
    public void mouseEntered(MouseEvent e) 
    {
    	if(no.getText().equals("请输入五位学号"))
    	{    		
    		no.setText("");
    		this.setVisible(true);
    	}    	
    }
    public void mousePressed(MouseEvent e) { }
    public void mouseExited(MouseEvent e) { }
}

